﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBModel;
using ZSDDBHelper;
using System.Web;
using System.IO;

namespace BllService
{
    public class FileManageService : DbCommon
    {
        #region 查询
        /// <summary>
        /// 查询文件列表
        /// </summary>
        /// <param name="model"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public string GetFileList(FileManageModel model,string Word, int page = 0, int limit = 0)
        {
            if (model == null)
                return "";
            var searchModel = new SearchSQLModel();
            searchModel.MainTable = SYS_Table.FileManage.ToString();
            searchModel.PageQty = limit;
            searchModel.Page = page;
            searchModel.isPaging = limit > 0 ? true : false;
            StringBuilder conditionStr = new StringBuilder();
            if (!string.IsNullOrEmpty(model.FileName))
            {
                conditionStr.Append(" AND FileName like '%" + model.FileName + "%'");
            }
            if (!string.IsNullOrEmpty(model.UploadUser))
            {
                conditionStr.Append(" AND UploadUser = '" + model.UploadUser + "'");
            }
            if (!string.IsNullOrEmpty(Word))
            {
                conditionStr.Append(" AND  ( FileName like '%" + Word + "%'");
                conditionStr.Append(" OR UploadUser LIKE '%" + Word + "%'");
                conditionStr.Append(" OR FileDesc LIKE '%" + Word + "%')");
            }
            searchModel.FilterCondition = conditionStr.ToString();
            var list = base.SearchData<FileManageModel>(searchModel);
            return base.SerializeToResultModel(list, searchModel.SumQty);
        }

        #endregion
        #region 新增/修改
        public string AddFileManage(FileManageModel model)
        {
            if (model == null)
            {
                return "";
            }
            var result = new ResultModel();
            model.UploadUser = (User.sessionItems["UserCode"] ?? "").ToString();
            if (string.IsNullOrEmpty(model.ID))
            {
                model.ID = Utility.GuidFormat.NewGuidN;
                model.UploadDate = DateTime.Now;
                result = base.InserTable(SYS_Table.FileManage.ToString(), model);
                result.data = model.ID;
            }
            else
            {
                result = base.UpdateTableByKey(SYS_Table.FileManage.ToString(), model);
                result.data = model.ID;
            }
            return base.SerializeObject(result);
        }
        #endregion

        #region 删除
        public string DeleteFileManage(FileManageModel model)
        {
            if (model == null)
                return "";
            listCondition.Add(new ConditionModel(){Name="ID",Value=model.ID});
            var result = base.DeleteTable(SYS_Table.FileManage.ToString(), listCondition);

            var fileList = base.DeserializeObject<List<filemodle>>(model.FilePath);
            foreach (var item in fileList)
            {
                var filename = item.url.Split('/')[item.url.Split('/').Length - 1];
                var filePath = AppDomain.CurrentDomain.BaseDirectory + "\\TempFile\\FileManage" + "\\" + filename;
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }

            
            return base.SerializeObject(result);
        }
        #endregion

    }
    
    public class filemodle 
    {
        public string name { get; set; }
        public string url { get; set; }
    }
}
